package com.pengheng.controller;

import com.pengheng.pojo.Employee;
import com.pengheng.pojo.User;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @remark
 * @Author pengheng
 * @date 2021/5/8 13:58
 */
@RestController
@RequestMapping("/mq")
public class RabbitMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq() {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                User user = new User();
                user.setId(1L);
                user.setAge(18);
                user.setName("张三" + i);
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", user, new CorrelationData(UUID.randomUUID().toString()));
            } else {
                Employee user = new Employee();
                user.setId(1L);
                user.setAge(18);
                user.setName("打工仔" + i + "号");
                rabbitTemplate.convertAndSend("hello-java-exchange", "hello22.java", user, new CorrelationData(UUID.randomUUID().toString()));

            }
        }
        return "ok";
    }

    @GetMapping("/test/createOrder")
    public String createOrderTest() {
        User user = new User();
        user.setAge(1000);
        user.setId(1000L);
        user.setName("延时队列" + new Date());
        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", user);
        return "发送成功";
    }
}
